using System;
using System.Collections.Generic;
using System.Text;
using Xunit;
using EventAbstractionAPI.RuleEngine;
using System.Threading;
//using Rhino.Mocks;

//The commandline for the xunit app, to test this assebly.
//C:\Program Files\xunit>xunit.console.exe "C:\Documents and Settings\rhwilbur\Desktop\Rule chaining\TestWorkFlowLibrary\WorkFlowTests\bin\Debug\WorkFlowTests.dll"
namespace WorkFlowTests
{
    public class SeqEventUseCase
    {

        #region Constructor
        public SeqEventUseCase()
        {
            _eventAlgebra.RaiseEvent += new Fire(EventAlgebra_RaiseEvent);
        }

        void EventAlgebra_RaiseEvent(List<ICapturedEvent> expiredEvents)
        {
            Console.WriteLine("The Output:");
            foreach (ICapturedEvent c in expiredEvents)
            {
                Console.WriteLine(":" + c.EventName + c.ComplexName);
            }
            _eventFired = true;
            _receivedResults.AddRange(expiredEvents);
        }

        #endregion

        [Fact]
        public void Test1()
        {
            //add a complex event. E1 <-- E2;E3
            //100,000,000 nano seconds = 0.1 of a second
            _eventAlgebra.Register(new SeqEvent(new TimeSpan(0,0,0,2,0),"ComplexEvent", "eventType2", "eventType3"));

            //Use the mock framework to create fake test objects
            ICapturedEvent event1 = new CapturedEvent();
            event1.EventName = "eventType2";

            ICapturedEvent event2 = new CapturedEvent();
            event2.EventName = "eventType3";

            ICapturedEvent event3 = new CapturedEvent();
            event3.EventName = "eventType2";

            ICapturedEvent event4 = new CapturedEvent();
            event4.EventName = "eventType3";

            ICapturedEvent event5 = new CapturedEvent();
            event5.EventName = "eventType3";

            ICapturedEvent event6 = new CapturedEvent();
            event6.EventName = "eventType3";

            _eventAlgebra.Receive(event1);
            _eventAlgebra.Receive(event2);
            _eventAlgebra.Receive(event3);
            _eventAlgebra.Receive(event4);
            _eventAlgebra.Receive(event5);
            Thread.Sleep(10);
            _eventAlgebra.Receive(event6);

            _expectedResults.Add("ComplexEvent");
            _expectedResults.Add("ComplexEvent");
            _expectedResults.Add("eventType3");
            _expectedResults.Add("eventType3"); //note the last event is currently ignored.

            int i = 0;
            while (_receivedResults.Count < _expectedResults.Count )
            {
                Thread.Sleep(10); // pause for 1 second = 1000;

                if (i > 80)
                {
                    break;
                }
                else
                {
                    i++;
                }
            }

            Assert.True(_eventFired,"No events were received");

            //compare the results here

            Assert.True(_receivedResults.Count == _expectedResults.Count, "Different number of results than expected. We got: " + _receivedResults.Count.ToString() + "we expected: " + _expectedResults.Count.ToString());
            



            for (int j = 0; j < _expectedResults.Count; j++)
            {
                if (!(_expectedResults[j] == _receivedResults[j].ComplexName || _expectedResults[j] == _receivedResults[j].EventName))
                {
                    _ResultsCorrect = false;
                    break;
                }
            }

            Assert.True(_ResultsCorrect, 
                        "The results arent as expected. Event Expirery could cause this, check the life times for your event types. We receieved the following " + _receivedResults.Count.ToString() + " items:" + _receivedResults[0].EventName + _receivedResults[0].ComplexName + "," + _receivedResults[1].EventName + _receivedResults[1].ComplexName + "," + _receivedResults[2].EventName + _receivedResults[2].ComplexName);

        }

        //MockRepository _mocks = new MockRepository();
        List<String> _expectedResults = new List<String>();
        List<ICapturedEvent> _receivedResults = new List<ICapturedEvent>();
        Boolean _eventFired = false;
        Boolean _ResultsCorrect = true;
        IEventAlgebra _eventAlgebra = new EventAlgebra();
    }
}
